//
//  ScanPayEngine.m
//  HuLaQuan
//
//  Created by hok on 1/27/16.
//  Copyright © 2016 Jianghao. All rights reserved.
//

#import "ScanPayEngine.h"

#import "CurrentUser.h"

@implementation ScanPayEngine

/**
 *  返回数据存储在self.orderInfo 中包括
 *  txnAmt: 交易金额
 *  payAmt: 支付金额(实际需要支付的金额)
 *
 *  appCode = QrCodePay;
 *  mchntId = 443701060120050;
 *  mchntPre = "0|0";
 *  notifySysName = QrCode;
 *  notifySysUrl = "http://218.201.197.90:8081/GzbdQrCode/servlet/XmlServlet";
 *  orderId = 1602030000027033;
 *  orderSummary = "\U8ba2\U5355\U67e5\U8be2";
 *  payAmt = 1;
 *  payTag = 1;
 *  resChannelId = 85100001;
 *  resId = 1000001;
 *  resName = jiaoyishui;
 *  resType = 0080;
 *  txnAmt = 1;
 *
 *  @param scanInfo 存储在self.scanInfo中，包括 discountCode，orderID，payType
 */
- (void)getOrderInfo:(NSDictionary *)scanInfo
{
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"getOrder\" sysName=\"attach\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@\" userId=\"%@\" channelId=\"88888888\"><orderId>%@</orderId><discountCode>%@</discountCode><payType>%@</payType><accType></accType><attach>attach</attach></msg>",[self getSerialNumber],[CurrentUser sharedInstance].token,[CurrentUser sharedInstance].ID ,scanInfo[@"orderID"],scanInfo[@"discountCode"],scanInfo[@"payType"]];
    
    self.scanInfo = scanInfo;
    
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",[self firstString],[self secondStringWithKey:randomKey],[self thirdStringWithBody:src andKey:randomKey]];

    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_QRCODE parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            
            NSString *resString = [_DES decrypt:[item lastObject] withKey:randomKey];
            
            NSDictionary *resultDic = [self parseXMLFromYinLianConnection:resString];
            
            
            
            NSLog(@"%@",resultDic);
            
            self.orderInfo = resultDic;
            
            result = @{@"isSuccess":@YES,@"result":resultDic};
        }else{
            result = @{@"isSuccess":@NO,@"message":item};
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"GetScanOrderInfoFinish" object:result];
    }];
}
/**
 *  返回数据存储在self.paymentInfo 中包括
 *
 *
 *   bankPre = "0|0";  银行折扣: 折扣金额|折扣说明
 *   orderId = 1602030000027033;
 *   payAmt = 1;  应付金额
 *   platPre = "0|0";  平台折扣: 折扣金额|折扣说明
 *   txnAmt = 1;  交易金额
 *  @param card  选择用来支付的银行卡的相关信息
 */
- (void)getPaymentInfo:(NSDictionary *)card
{
    
    NSLog(@"%@",card);
    self.chosenCard = card;
    
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"getPayInfo\" sysName=\"attach\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@\" userId=\"%@\" channelId=\"88888888\" ><orderId>%@</orderId><discountCode>%@</discountCode><payType>%@</payType><accType>%@</accType><cardId>%@</cardId><txnAmt>%@</txnAmt><payAmt>%@</payAmt><appCode>%@</appCode><resType>%@</resType><attach>attach</attach></msg>",[self getSerialNumber],[CurrentUser sharedInstance].token,[CurrentUser sharedInstance].ID ,self.scanInfo[@"orderID"],self.scanInfo[@"discountCode"],self.scanInfo[@"payType"],card[@"accType"],card[@"cardId"],self.orderInfo[@"txnAmt"],self.orderInfo[@"payAmt"],self.orderInfo[@"appCode"],self.orderInfo[@"resType"]];
    NSLog(@"%@ -- src --- ",src);
    
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",[self firstString],[self secondStringWithKey:randomKey],[self thirdStringWithBody:src andKey:randomKey]];
    
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_CASHIER parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            NSString *resString = [_DES decrypt:[item lastObject] withKey:randomKey];
            
            NSDictionary *resultDic = [self parseXMLFromYinLianConnection:resString];
            
            self.paymentInfo = resultDic;
            
            NSLog(@"%@",resultDic);
            
            result = @{@"isSuccess":@YES};
        }else{
            result = @{@"isSuccess":@NO,@"message":item};
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"GetScanPaymentInfoFinish" object:result];
    }];
}
- (void)payScanOrder:(NSDictionary *)passwordInfo
{
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"payOrderA\" sysName=\"attach\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@|%@\" userId=\"%@\" channelId=\"88888888\" mchntId=\"%@\" ><orderId>%@</orderId><resType>%@</resType><resId>%@</resId><resName>%@</resName><payMode>0</payMode><payType>%@</payType><txnAmt>%@</txnAmt><payAmt>%@</payAmt><cardId>%@</cardId><appCode>%@</appCode><notifySysName>%@</notifySysName><noticeSysUrl>%@</noticeSysUrl><attach>attach</attach></msg>",[self getSerialNumber],[CurrentUser sharedInstance].token,passwordInfo[@"tokenValue"],[CurrentUser sharedInstance].ID ,self.orderInfo[@"mchntId"],self.orderInfo[@"orderId"],self.orderInfo[@"resType"],self.orderInfo[@"resId"],self.orderInfo[@"resName"],self.scanInfo[@"payType"],self.paymentInfo[@"txnAmt"],self.paymentInfo[@"payAmt"],self.chosenCard[@"cardId"],self.orderInfo[@"appCode"],self.orderInfo[@"notifySysName"],self.orderInfo[@"notifySysUrl"]];
    
    NSLog(@"%@ -- src --- ",src);
    
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",[self firstString],[self secondStringWithKey:randomKey],[self thirdStringWithBody:src andKey:randomKey]];
    
    NSLog(@"%@",final);
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_CASHIER parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            NSString *resString = [_DES decrypt:[item lastObject] withKey:randomKey];
            
            NSDictionary *resultDic = [self parseXMLFromYinLianConnection:resString];
            
            
            
            NSLog(@"%@",resultDic);
            
            result = @{@"isSuccess":@YES,@"result":resultDic};
        }else{
            result = @{@"isSuccess":@NO,@"message":item};
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"PayScanOrderFinish" object:result];
    }];

}
- (void)payScanOrderwithCard
{
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"payOrderA\" sysName=\"attach\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@\" userId=\"%@\" channelId=\"88888888\" mchntId=\"%@\" ><orderId>%@</orderId><resType>%@</resType><resId>%@</resId><resName>%@</resName><payMode>0</payMode><payType>%@</payType><txnAmt>%@</txnAmt><payAmt>%@</payAmt><cardId>%@</cardId><appCode>%@</appCode><notifySysName>%@</notifySysName><noticeSysUrl>%@</noticeSysUrl><attach>%@</attach></msg>",[self getSerialNumber],[CurrentUser sharedInstance].token,[CurrentUser sharedInstance].ID ,self.orderInfo[@"mchntId"],self.orderInfo[@"orderId"],self.orderInfo[@"resType"],self.orderInfo[@"resId"],self.orderInfo[@"resName"],self.scanInfo[@"payType"],self.paymentInfo[@"txnAmt"],self.paymentInfo[@"payAmt"],self.chosenCard[@"cardId"],self.orderInfo[@"appCode"],self.orderInfo[@"notifySysName"],self.orderInfo[@"notifySysUrl"],@{@"payLimit":@"true"}];
    
    NSLog(@"%@ -- src --- ",src);
    
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",[self firstString],[self secondStringWithKey:randomKey],[self thirdStringWithBody:src andKey:randomKey]];
    
    NSLog(@"%@",final);
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_CASHIER parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            NSString *resString = [_DES decrypt:[item lastObject] withKey:randomKey];
            
            NSDictionary *resultDic = [self parseXMLFromYinLianConnection:resString];
            
            
            
            NSLog(@"%@",resultDic);
            
            result = @{@"isSuccess":@YES,@"result":resultDic};
        }else{
            result = @{@"isSuccess":@NO,@"message":item};
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"PayScanOrderFinish" object:result];
    }];

}
@end
